package cn.bugstack.mybatis.session.defaults;

import cn.bugstack.mybatis.bindings.MapperRegistry;
import cn.bugstack.mybatis.executor.Executor;
import cn.bugstack.mybatis.session.Configuration;
import cn.bugstack.mybatis.session.SqlSession;
import cn.bugstack.mybatis.session.SqlSessionFactory;
import cn.bugstack.mybatis.transaction.Transaction;
import cn.bugstack.mybatis.transaction.TransactionFactory;
import cn.bugstack.mybatis.transaction.TransactionIsolationLevel;

public class DefaultSqlSessionFactory implements SqlSessionFactory {

    private final Configuration configuration;

    public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public SqlSession openSession() {
        TransactionFactory transactionFactory = configuration.getEnvironment().getTransactionFactory();
        Transaction transaction = transactionFactory.newTransaction(configuration.getEnvironment().getDataSource(), TransactionIsolationLevel.READ_COMMITTED, false);
        Executor executor = configuration.newExecutor(transaction);
        return new DefaultSqlSession(configuration,executor);
    }
}
